package com.changgou.goods.service.impl;
import com.changgou.goods.dao.LogAllMapper;
import com.changgou.goods.pojo.LogAll;
import com.changgou.goods.service.LogAllService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/****
 * @Author:shenkunlin
 * @Description:LogAll业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class LogAllServiceImpl implements LogAllService {

    @Autowired
    private LogAllMapper logAllMapper;


    /**
     * LogAll条件+分页查询
     * @param logAll 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<LogAll> findPage(LogAll logAll, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(logAll);
        //执行搜索
        return new PageInfo<LogAll>(logAllMapper.selectByExample(example));
    }

    /**
     * LogAll分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<LogAll> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<LogAll>(logAllMapper.selectAll());
    }

    /**
     * LogAll条件查询
     * @param logAll
     * @return
     */
    @Override
    public List<LogAll> findList(LogAll logAll){
        //构建查询条件
        Example example = createExample(logAll);
        //根据构建的条件查询数据
        return logAllMapper.selectByExample(example);
    }


    /**
     * LogAll构建查询对象
     * @param logAll
     * @return
     */
    public Example createExample(LogAll logAll){
        Example example=new Example(LogAll.class);
        Example.Criteria criteria = example.createCriteria();
        if(logAll!=null){
            // 
            if(!StringUtils.isEmpty(logAll.getId())){
                    criteria.andEqualTo("id",logAll.getId());
            }
            // 访问路径
            if(!StringUtils.isEmpty(logAll.getUrl())){
                    criteria.andEqualTo("url",logAll.getUrl());
            }
            // 类型
            if(!StringUtils.isEmpty(logAll.getType())){
                    criteria.andEqualTo("type",logAll.getType());
            }
            // 描述
            if(!StringUtils.isEmpty(logAll.getDoc())){
                    criteria.andEqualTo("doc",logAll.getDoc());
            }
            // 用户
            if(!StringUtils.isEmpty(logAll.getUser())){
                    criteria.andEqualTo("user",logAll.getUser());
            }
            // 访问时间
            if(!StringUtils.isEmpty(logAll.getDate())){
                    criteria.andEqualTo("date",logAll.getDate());
            }
            // 状态: 1 成功  ,2失败 ,3其他
            if(!StringUtils.isEmpty(logAll.getState())){
                    criteria.andEqualTo("state",logAll.getState());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        logAllMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改LogAll
     * @param logAll
     */
    @Override
    public void update(LogAll logAll){
        logAllMapper.updateByPrimaryKey(logAll);
    }

    /**
     * 增加LogAll
     * @param logAll
     */
    @Override
    public void add(LogAll logAll){
        logAllMapper.insert(logAll);
    }

    /**
     * 根据ID查询LogAll
     * @param id
     * @return
     */
    @Override
    public LogAll findById(Integer id){
        return  logAllMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询LogAll全部数据
     * @return
     */
    @Override
    public List<LogAll> findAll() {
        return logAllMapper.selectAll();
    }
}
